﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Reflection;

namespace PoserLoot.Web.Core
{
    public static class SelectListBuilder
    {
        public static SelectList Create<T>(IEnumerable<T> collection, string keyPropertyName, string valuePropertyName, object selected)
        {
            Dictionary<Guid, string> dict = new Dictionary<Guid, string>();

            PropertyInfo keyProperty = typeof(T).GetProperty(keyPropertyName);
            PropertyInfo valueProperty = typeof(T).GetProperty(valuePropertyName);

            foreach (var i in collection)
            {
                Guid key = new Guid(keyProperty.GetValue(i, null).ToString());
                string value = valueProperty.GetValue(i, null).ToString();

                dict.Add(key, value);
            }

            return new SelectList(dict, "Key", "Value", selected);
        }
    }
}
